<?php


namespace app\index\model;


use think\Model;

class ProductPurHistory extends Model
{
    protected $type=['1'=>'入库','2'=>'出库'];

    public static function getStockHistoryList($search_param,$page_param){
        $ProductPurHistoryClass = new self();

        $where['pph.del_flag']=['=',0];
        $where['p.del_flag']=['=',0];

        ['action_role'=>$action_role,'search_user'=>$search_user,'group'=>$group]=ActionRole::checkUserActionRole('product');
        if(!empty($group)){
            $warehouse_id=GroupWarehouse::getWareHouseByGroup($group);
        }else if (is_numeric($search_param['group_id']) && $search_param['group_id']) {
            $warehouse_id=GroupWarehouse::getWareHouseByGroup($search_param['group_id']);
        }
        if(isset($warehouse_id) && !empty($warehouse_id)){
            $where['p.warehouse']=['in',$warehouse_id];
        }
        if(!empty($search_user)) {
            $where['p.create_user'] = ['in', $search_user];
        }else if(is_numeric($search_param['user_id'])){
            $search_user=[$search_param['user_id']];
            $where['p.create_user'] = ['in', $search_user];
        }

        if (is_numeric($search_param['stock_warehouse'])) {
            $where['p.warehouse']=['=',$search_param['stock_warehouse']];
        }
        if (is_numeric($search_param['type'])) {
            $where['pph.type']=['=',$search_param['type']];
        }
        if (is_numeric($search_param['vendor_id'])) {
            $where['pph.vendor_id']=['=',$search_param['vendor_id']];
        }
        if (!empty($search_param['create_time'])) {
            $where['pph.create_time']=['between',[$search_param['create_time'].' 00:00:00',$search_param['create_time'].' 23:59:59']];
        }
        if (!empty($search_param['search_key']) && !empty($search_param['search_value'])) {
            $where['p.'.$search_param['search_key']]=['like','%'.$search_param['search_value'].'%'];
        }
        $obj=$ProductPurHistoryClass->alias('pph')
            ->join('product p','pph.pro_id=p.id','left')
            ->join('user u','pph.create_user=u.id','left')
            ->join('vendor v','pph.vendor_id=v.id','left')
            ->where($where)
            ->field('pph.id,p.cn_name,p.en_name,p.type product_type,pph.amount,pph.pur_price,pph.type,pph.create_time,u.user_name,v.company vendor')
            ->order('pph.create_time','desc');
        if(!empty($page_param)) {
            $obj=$obj->paginate($page_param['page_size'], false, ['page' => $page_param['page_num']]);
            $count=$obj->total();
            $data=$obj->items();
        }else{
            $data=$obj->select()->toArray();
            $count=0;
        }
        foreach($data as $key=>$value){
            $data[$key]['product_type_text']=Product::$product_type[$value['product_type']];
            $data[$key]['type_text']=$ProductPurHistoryClass->type[$value['type']];
        }
        return ['status'=>true,'result'=>['product'=>$data,'total'=>$count,'action_role'=>$action_role]];
    }

    public static function getStockHistoryByOrderId($order_id): array
    {
        $ProductPurHistoryClass=new self();
        $obj=$ProductPurHistoryClass->alias('puh')
            ->join('product p','puh.pro_id=p.id','left')
            ->where('puh.order_id',$order_id)
            ->where('puh.del_flag',0)
            ->where('puh.type',2)
            ->where('puh.order_item_id','<>',0)
            ->field('puh.pro_id,puh.amount,puh.order_item_id,p.cn_name,p.type')
            ->select();
        return $obj->toArray();
    }

    public static function getStockOutPurCost($search_param): array
    {
        $ProductPurHistoryClass=new self();
        $where['del_flag']=['=',0];
        $where['type']=['=',2];
        if (!empty($search_param['create_time'])) {
            $where['create_time']=['between',[$search_param['create_time'][0].' 00:00:00',$search_param['create_time'][1].' 23:59:59']];
        }
        $obj=$ProductPurHistoryClass
            ->where($where)
            ->sum('amount*pur_price');
        if(is_null($obj)){
            $pur_cost=0;
        }else{
            $pur_cost=$obj;
        }
        return ['status'=>true,'result'=>['cost'=>$pur_cost]];
    }

    public static function getSalesAmount($search_param,$page_param,$sort_param): array
    {
        $ProductPurHistoryClass=new self();

        $where['pph.del_flag']=['=',0];
        $where['pph.type']=['=',2];
        if (!empty($search_param['create_time'])) {
            $where['pph.create_time']=['between',[$search_param['create_time'][0].' 00:00:00',$search_param['create_time'][1].' 23:59:59']];
        }
        $where['p.type']=['=',2];

        $obj=$ProductPurHistoryClass->alias('pph')
            ->join('__PRODUCT__ p','pph.pro_id=p.id','left')
            ->where($where)
            ->field('round(sum(((pph.selling_price*'.usdExchange.')-pph.pur_price)*pph.amount),2) profit,round(sum(pph.amount),2) amount')
            ->select();
        if($obj->isEmpty()){
            $total_sales=[];
        }else{
            $total_sales=$obj->toArray();
        }

        $obj=$ProductPurHistoryClass
            ->where($where)->alias('pph')
            ->join('__PRODUCT__ p','pph.pro_id=p.id','left')
            ->field('p.cn_name,pph.pro_id,round(sum(((pph.selling_price*'.usdExchange.')-pph.pur_price)*pph.amount),2) profit,round(sum(pph.amount),2) amount')
            ->group('pph.pro_id')
            ->order($sort_param['column'],$sort_param['desc'])
            ->paginate($page_param['page_size'],false,['page'=>$page_param['page_num']]);
        $count=$obj->total();
        $sales_data=$obj->items();

        return ['status'=>true,'result'=>['count'=>$count,'total_sales'=>$total_sales,'sales_data'=>$sales_data]];
    }
}